package com.clothing.customization.controller.admin;

import com.clothing.customization.dto.ApiResult;
import com.clothing.customization.service.OrderManagementService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单管理控制器
 */
@RestController
@RequestMapping("/api/admin/order")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class OrderManagementController {

    private final OrderManagementService orderManagementService;

    /**
     * 获取订单统计数据
     * @return 统计数据
     */
    @GetMapping("/statistics")
    public ResponseEntity<ApiResult<Map<String, Object>>> getOrderStatistics() {
        Map<String, Object> statistics = orderManagementService.getOrderStatistics();
        return ResponseEntity.ok(ApiResult.success(statistics));
    }

    /**
     * 获取订单列表
     * @param status 状态
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param keyword 关键词
     * @param page 页码
     * @param size 每页数量
     * @return 订单列表
     */
    @GetMapping("/list")
    public ResponseEntity<ApiResult<Map<String, Object>>> getOrderList(
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> orders = orderManagementService.getOrderList(status, startDate, endDate, keyword, page, size);
        return ResponseEntity.ok(ApiResult.success(orders));
    }

    /**
     * 获取订单详情
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResult<Map<String, Object>>> getOrderDetail(@PathVariable Long id) {
        Map<String, Object> orderDetail = orderManagementService.getOrderDetail(id);
        return ResponseEntity.ok(ApiResult.success(orderDetail));
    }

    /**
     * 更新订单状态
     * @param id 订单ID
     * @param status 状态
     * @param remark 备注
     * @return 是否成功
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<ApiResult<Boolean>> updateOrderStatus(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) String remark) {
        boolean result = orderManagementService.updateOrderStatus(id, status, remark);
        return ResponseEntity.ok(ApiResult.success(result));
    }

    /**
     * 获取订单流程记录
     * @param orderId 订单ID
     * @return 流程记录列表
     */
    @GetMapping("/{orderId}/process-logs")
    public ResponseEntity<ApiResult<List<Map<String, Object>>>> getOrderProcessLogs(@PathVariable Long orderId) {
        List<Map<String, Object>> logs = orderManagementService.getOrderProcessLogs(orderId);
        return ResponseEntity.ok(ApiResult.success(logs));
    }

    /**
     * 添加订单流程记录
     * @param orderId 订单ID
     * @param processType 流程类型
     * @param content 内容
     * @param operatorId 操作人ID
     * @return 是否成功
     */
    @PostMapping("/{orderId}/process-log")
    public ResponseEntity<ApiResult<Boolean>> addOrderProcessLog(
            @PathVariable Long orderId,
            @RequestParam String processType,
            @RequestParam String content,
            @RequestParam Long operatorId) {
        boolean result = orderManagementService.addOrderProcessLog(orderId, processType, content, operatorId);
        return ResponseEntity.ok(ApiResult.success(result));
    }

    /**
     * 获取订单数据分析
     * @param analysisType 分析类型: daily, weekly, monthly, category, region
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 分析数据
     */
    @GetMapping("/analysis")
    public ResponseEntity<ApiResult<Map<String, Object>>> getOrderAnalysisData(
            @RequestParam(defaultValue = "daily") String analysisType,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        Map<String, Object> data = orderManagementService.getOrderAnalysisData(analysisType, startDate, endDate);
        return ResponseEntity.ok(ApiResult.success(data));
    }

    /**
     * 获取未处理纠纷列表
     * @param page 页码
     * @param size 每页数量
     * @return 纠纷列表
     */
    @GetMapping("/disputes/pending")
    public ResponseEntity<ApiResult<Map<String, Object>>> getPendingDisputeList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> disputes = orderManagementService.getPendingDisputeList(page, size);
        return ResponseEntity.ok(ApiResult.success(disputes));
    }

    /**
     * 获取所有纠纷列表
     * @param status 状态: pending, processing, resolved, rejected
     * @param page 页码
     * @param size 每页数量
     * @return 纠纷列表
     */
    @GetMapping("/disputes")
    public ResponseEntity<ApiResult<Map<String, Object>>> getAllDisputeList(
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> disputes = orderManagementService.getAllDisputeList(status, page, size);
        return ResponseEntity.ok(ApiResult.success(disputes));
    }

    /**
     * 获取纠纷详情
     * @param id 纠纷ID
     * @return 纠纷详情
     */
    @GetMapping("/dispute/{id}")
    public ResponseEntity<ApiResult<Map<String, Object>>> getDisputeDetail(@PathVariable Long id) {
        Map<String, Object> dispute = orderManagementService.getDisputeDetail(id);
        return ResponseEntity.ok(ApiResult.success(dispute));
    }

    /**
     * 处理纠纷
     * @param id 纠纷ID
     * @param status 状态: processing, resolved, rejected
     * @param resolution 解决方案
     * @param operatorId 操作人ID
     * @return 是否成功
     */
    @PutMapping("/dispute/{id}")
    public ResponseEntity<ApiResult<Boolean>> processDispute(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String resolution,
            @RequestParam Long operatorId) {
        boolean result = orderManagementService.processDispute(id, status, resolution, operatorId);
        return ResponseEntity.ok(ApiResult.success(result));
    }

    /**
     * 删除订单
     * @param id 订单ID
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResult<Boolean>> deleteOrder(@PathVariable Long id) {
        boolean result = orderManagementService.deleteOrder(id);
        return ResponseEntity.ok(ApiResult.success(result));
    }
} 